const fs_extra = require('fs-extra');
const path = require('path');

const Config = {};
const MAIN_PATH = process.pkg? path.dirname(process.execPath) : path.resolve(__dirname, '../');
Config.SOFTWARE_PATH = path.resolve(MAIN_PATH, 'config.json');
Config.SOFTWARE_DEFAULT = {
    "appDir": "/app",
    "static": {
        "http": {
            "port": 8080
        },
        "https": {
            "port": 443
        }
    },
    "debug": true,
    "formatJSON": true,
};

Config.get = (inPath) => {
    return fs_extra.readJsonSync(inPath, { throws: false }) ?? {};
}

Config.SOFTWARE = {
    ...Config.SOFTWARE_DEFAULT,
    ...Config.get(Config.SOFTWARE_PATH)
};

Config.SOFTWARE.appDir = path.resolve(MAIN_PATH, Config.SOFTWARE.appDir);
Config.SOFTWARE.mainDir = MAIN_PATH;

module.exports = Config;